#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<string.h>

int main()
{
    int fd_first[2];
    int fd_second[2];
    if(pipe(fd_first)==-1)
    {
        perror("pipe:");
    }
    if(pipe(fd_second)==-1)
    {
        perror("pipe:");
    }
    pid_t pid;
    pid=fork();
    if(pid<0)
    {
        perror("fork:");
        exit(-1);
    }
    else if(pid==0)
    {
        sleep(1);
        while(1)
        {
            char temp[100]={0};
            read(fd_first[0],temp,sizeof(temp));
            printf("parent said to child:%s\n",temp);
            memset(temp,0,sizeof(temp));
            printf("please input message to parent:");
            scanf("%[^\n]",temp);
            getchar();
            write(fd_second[1],temp,sizeof(temp));
        }
    }
    else
    {
        while(1)
        {
            printf("please input message to child:");
            char temp[100]={0};
            scanf("%[^\n]",temp);
            getchar();
            write(fd_first[1],temp,sizeof(temp));
            memset(temp,0,sizeof(temp));
            read(fd_second[0],temp,sizeof(temp));
            printf("child said to parent:%s\n",temp);
        }
    }
    return 0;
}